#include "WebServer.hh"

using namespace std;

MyWebServer *server;

static
DWORD WINAPI Thread_URL (void *m)
{
	MyWebServer *me;

    printf("Setting up callbacks in thread\n");
	me = reinterpret_cast<MyWebServer *>(m);
    me->urlhook->set_url_cb(QUERY_URL, new  query_url_called_cb(&me->querycb, me));
    me->urlhook->set_url_cb(RESET_URL, new  reset_url_called_cb(&me->resetcb, me));
    me->urlhook->set_url_cb(START_URL, new  start_url_called_cb(&me->startcb, me));
    me->urlhook->set_url_cb(STOP_URL,  new  stop_url_called_cb(&me->stopcb, me));
    return me->urlhook->evt_loop();
} 


// Constructor
MyWebServer::MyWebServer ()
{
    ftdi_device = new FTDI_Device;
    urlhook = new UrlHook;
    // Attach callbacks
//    query_url_called_cb *cb;
//    cb = new  query_url_called_cb(&querycb, this);

    // Start off the WebServer
	DWORD dwThreadId; 
    HANDLE hThread; 

    hThread = CreateThread( 
        NULL,                        // default security attributes 
        0,                           // use default stack size  
        Thread_URL,               // thread function 
        (void *)this,                        // argument to thread function 
        0,                           // use default creation flags 
        &dwThreadId);                // returns the thread identifier 
 
   // Check the return value for success. 
 
   if (hThread == NULL) {
	   printf("Thread Create failed\n");
	   exit (-1);
   }
}

int main(void)
{
// Create a webserver
    server = new MyWebServer;

    for (;;);
}
// Define Url Hooks
int
QueryUrlHookCB::operator() (query_url_called_cb *mycb)
{
    char str[100];
    printf("Query Called\n");
    sprintf(str, "%s %s\n",
            "Device found = ",
            mycb->cookie()->ftdi_device->device_present() ? "True" : " False");
    mycb->cookie()->urlhook->reply_browser(str);
    return 0;
}

int
ResetUrlHookCB::operator() (reset_url_called_cb *mycb)
{
    printf("Reset Called\n");
    mycb->cookie()->urlhook->reply_browser("Reset\n");
    return 0;
}

int
StartUrlHookCB::operator() (start_url_called_cb *mycb)
{
    FTDI_Device *chip;

    printf("Start Called\n");
    chip = mycb->cookie()->ftdi_device;
    chip->pin_set(0, FTDI_Device::OUTPUT); chip->pin_set(1, FTDI_Device::OUTPUT);
    chip->pin_set(2, FTDI_Device::OUTPUT); chip->pin_set(3, FTDI_Device::OUTPUT);
    chip->pin_write(0, true); chip->pin_write(1, true);
    chip->pin_write(2, true); chip->pin_write(3, true);
    mycb->cookie()->urlhook->reply_browser("Start Motor Cycle\n");
    return 0;
}

int
StopUrlHookCB::operator() (stop_url_called_cb *mycb)
{
    FTDI_Device *chip;

    printf("Stop Called\n");
    chip = mycb->cookie()->ftdi_device;
    chip->pin_set(0, FTDI_Device::INPUT); chip->pin_set(1, FTDI_Device::INPUT);
    chip->pin_set(2, FTDI_Device::INPUT); chip->pin_set(3, FTDI_Device::INPUT);

    mycb->cookie()->urlhook->reply_browser("Stop\n");
    return 0;
}
